﻿using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.ObjectMapping;
using Abp.UI;
using Camc.Quality.Authorization.Users;
using Camc.Quality.IOperationHistory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Camc.Quality.OperationHistory
{
    /// <summary>
    /// 操作履历
    /// </summary>
    public class OperationHistoryAppService : QualityAppServiceBase, IOperationHistoryAppService
    {
        private readonly IRepository<OperationLog, Guid> _operationHistoryRepository;
        private readonly IObjectMapper _objectMapper;

        public OperationHistoryAppService(
           IObjectMapper objectMapper,
           IRepository<User, long> userRepository,
           IRepository<OperationLog, Guid> operationHistoryRepository)
        {
            _operationHistoryRepository = operationHistoryRepository;
            _objectMapper = objectMapper;

        }
        /// <summary>
        /// 插入操作履历
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateOrUpdateOperationLog(OperationLogDto input)
        {
            var operationLog = _objectMapper.Map<OperationLog>(input);
            await _operationHistoryRepository.InsertAsync(operationLog);
        }





        /// <summary>
        /// 系统插入操作履历接口
        /// </summary>
        /// <param name="businessId"></param>
        /// <param name="businessType"></param>
        /// <param name="operationType"></param>
        /// <param name="content"></param>
        /// <param name="PadId"></param>
        public void InsertOperationLog(Guid businessId, int businessType, int operationType, string content, string PadId = "")
        {
            try
            {
                OperationLog log = new OperationLog();
                log.BusinessId = businessId;
                log.BusinessType = (BusinessTypes)businessType;
                log.OperationType = (OperationTypes)operationType;
                log.Content = content;
                if (!string.IsNullOrEmpty(PadId))
                {
                    log.UserName = PadId;
                }
                else
                {
                    log.UserName = GetCurrentUser().Surname + GetCurrentUser().Name;
                }
                _operationHistoryRepository.Insert(log);
            }
            catch (Exception msg)
            {
                throw new UserFriendlyException("身份验证过期或者失效！请重新登录！");
            }

        }
        [UnitOfWork]
        public void InsertOperationLogNew(Guid businessId, BusinessTypes businessType, OperationTypes operationType, string content, string padId = "", Guid? stepId = null)
        {
            try
            {
                OperationLog log = new OperationLog();
                log.BusinessId = businessId;
                log.BusinessType = businessType;
                log.OperationType = operationType;
                log.Content = content;
                if (!string.IsNullOrEmpty(padId))
                {
                    log.UserName = padId;
                }
                else
                {
                    log.UserName = GetCurrentUser().Surname + GetCurrentUser().Name;
                    log.UserId = GetCurrentUser().Id;
                }
                log.StepId = stepId;
                _operationHistoryRepository.Insert(log);
            }
            catch (Exception msg)
            {
                throw new UserFriendlyException("身份验证过期或者失效！请重新登录！");
            }

        }

        /// <summary>
        /// 根据businessId查询操作履历  type=1 质量 type=2 普通规程
        /// </summary>
        /// <param name="businessId"></param>
        /// <returns></returns>
        public async Task<List<OperationLogDto>> GetOperationLogList(Guid businessId)
        {
            if (string.IsNullOrEmpty(businessId.ToString()))
            {
                throw new Abp.UI.UserFriendlyException("查询条件异常，请重新加载!");
            }
            var logList = _operationHistoryRepository.GetAll().Where(d => d.BusinessId == businessId).ToList();

            if (logList.Count > 0)
            {

                logList = logList.OrderByDescending(d => d.CreationTime).ToList();
                return _objectMapper.Map<List<OperationLogDto>>(logList);
            }
            else
            {
                List<OperationLogDto> optList = new List<OperationLogDto>();
                return optList;
            }
        }
        public async Task<List<OperationLogDto>> GetTechOperationLogList(Guid businessId, int type = 0)
        {
            if (string.IsNullOrEmpty(businessId.ToString()))
            {
                throw new Abp.UI.UserFriendlyException("查询条件异常，请重新加载!");
            }
            var logList = _operationHistoryRepository.GetAll().Where(d => d.BusinessId == businessId).ToList();

            if (logList.Count > 0)
            {
                if (type == 1)
                {
                    logList = logList.Where(d => d.BusinessType == BusinessTypes.Bop质量执行记录).ToList();
                }
                if (type == 2)
                {
                    logList = logList.Where(d => d.BusinessType == BusinessTypes.Bop质量变更记录).ToList();
                }
                logList = logList.OrderByDescending(d => d.CreationTime).ToList();
                return _objectMapper.Map<List<OperationLogDto>>(logList);
            }
            else
            {
                List<OperationLogDto> optList = new List<OperationLogDto>();
                return optList;
            }
        }


    } 
}
